# coding=utf-8
import random
import sys

from scrapy_sinahealth import settings

reload(sys)
sys.setdefaultencoding("utf-8")


class Customizer(object):

    def get_default_random_user_agents(self):
        user_agent_pool_list = settings.USER_AGENTS;
        return self.get_random_user_agents(user_agent_pool_list)

    def get_random_user_agents(self, user_agents):
        user_agent = random.choice(user_agents)

        return user_agent

    def get_headers(self):
        r = self.get_default_random_user_agents()
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            # 'Cache-Control': 'max-age=0',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4',
            # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
             'User-Agent': r,
        }
        return headers

    def get_download_delay(self):
        rand = random.random() * 3  # generate a random number between 0 to 2 seconds
        return rand

    def get_banned_status_code_list(self):
        return ["502", "503"]

    def get_banned_sleep(self):
        '''
        (in seconds) get sleep time after specific return status code such as 502
        :return:
        '''
        return 600;


class WechatCustomizer(Customizer):
    def get_headers(self):
        r = self.get_random_user_agents(settings.USER_AGENTS_WECHAT)
        headers = {
            # 'Host': 'img1.gtimg.com',
            # "Referer": "http://health.qq.com/a/20180102/006424.htm",
            'User-Agent': r,
            # 'Upgrade-Insecure-Requests': 1,
            'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
        }
        return headers

    def get_download_delay(self):
        rand = random.random() * 2  # generate a random number between 0 to 2 seconds
        return rand

class EchinalifeCustomizer(Customizer):
    def get_headers(self):
        r = self.get_default_random_user_agents()
        headers = {
            'User-Agent': r,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
            'Proxy-Connection': 'keep-alive'
        }

        return headers;

class QQHealthCustomizer(Customizer):
    def get_headers(self):
        r = self.get_default_random_user_agents()
        headers = {
            # 'Host': 'img1.gtimg.com',
            "Referer": "http://health.qq.com/a/20180102/006424.htm",
            'User-Agent': r,
            # 'Upgrade-Insecure-Requests': 1,
            'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
            'Proxy-Connection': 'keep-alive'
        }

        return headers;


class TwentyFirstEconomyCustomizer(Customizer):
    def get_headers(self):
        r = self.get_default_random_user_agents()
        headers = {
            'User-Agent': r,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
            'Proxy-Connection': 'keep-alive'
        }
        return headers


class WinbaoxianCustomizer(Customizer):
    def get_headers(self):
        r = self.get_default_random_user_agents()
        headers = {
            'User-Agent': r,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
            'Proxy-Connection': 'keep-alive'
        }
        return headers
